package org.openfans.domain;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.openfans.persistence.Fetcher;
import org.openfans.persistence.Persistence;
import org.openfans.util.ThreadFans;

public class Facade {
	private Fetcher fetcher;

	private Persistence persistence;

	/**
	 * 功能说明：用户收藏一个文章<br>
	 * 创建者：张俊<br>
	 * 创建时间：2006-5-31<br>
	 * 
	 * @param fans
	 * @param article
	 * @param tag
	 */
	public void addCollectArticle(Fans fans, Article article, Tag tag) {

	}

	/**
	 * 功能说明：用户添加到小组中，为方便使用事务，采用add前缀<br>
	 * 创建者：张俊<br>
	 * 创建时间：2006-5-3<br>
	 * 
	 * @param group
	 * @param user
	 * @return
	 */
	public Group addFansToGroup(Group group, Fans user) {
		user.addGroup(group);
		return group;
	}

	/**
	 * 增加一个推荐的文章
	 * 
	 * @param fans
	 * @param feedItem
	 * @param tag
	 */
	public void saveArticleByFeedItem(Fans fans, FeedItem feedItem,
			String tagStr, Article article) {

		Set<Tag> tags = getTagSet(tagStr, fans);

		fans.saveFeedItem(feedItem, tags, article);
	}

	/**
	 * 从fans输入的tag串生成相应的tag
	 * 
	 * @param tagStrs
	 * @param fans
	 * @return
	 */
	public Set<Tag> getTagSet(String tagStr, Fans fans) {
		String[] tagStrs = tagStr.split(" ");
		Set<Tag> tags = new HashSet<Tag>();
		for (String s : tagStrs) {
			// 防止多个空格，把空格作为tag
			if (s.trim().length() > 0) {
				Tag tag = getTagByTitle(s);
				if (null == tag) {
					tag = addNewTagByTitle(fans, s);
				} else {
					addFansTag(fans, tag);
				}
				tags.add(tag);
			}
		}
		return tags;
	}

	public Auth authenticate(Auth auth) {
		return fetcher.authenticate(auth);
	}

	public Fans authenticateFans(String value) {
		return fetcher.authenticateFans(value);
	}

	public Article getArticle(int id) {
		return fetcher.getArticle(id);
	}

	public Auth getAuthByActivateCode(String code) {
		return fetcher.getAuthByActivateCode(code);
	}

	public Fans getFans(int id) {
		return fetcher.getFans(id);
	}

	public Fans getFans(String name) {
		return fetcher.getFans(name);
	}

	public Feed getFeed(int id) {
		return fetcher.getFeed(id);
	}

	public FeedItem getFeedItem(int id) {
		return fetcher.getFeedItem(id);
	}

	public Group getGroup(int id) {
		return fetcher.getGroup(id);
	}

	public Message getMessage(int id) {
		return fetcher.getMessage(id);
	}

	/**
	 * 只需要最前面的文章,不需分页支持,所以单独了一个接口,避免执行count和分页运算
	 * 
	 * @param pageSize
	 * @param order
	 * @param type
	 * @return
	 */
	public List getRecentArticles(int pageSize, String order, String type) {
		return fetcher.getRecentArticles(pageSize, order, type);
	}

	public Page searchFansSavedArticles(int fansId, String queryStr,
			int pageNum, int pageSize, String order) {
		return fetcher.searchFansSavedArticles(fansId, queryStr, pageNum,
				pageSize, order);
	}

	public Page searchArticles(String query, String order, String type,
			int pageNum, int pageSize) {
		return fetcher.searchArticles(query, order, type, pageNum, pageSize);
	}

	public Page getPagedFans(int pageNum, int pageSize, String order) {
		return fetcher.getPagedFans(order, pageNum, pageSize);
	}

	public Page getPagedFeedItems(String order, int pageNum, int pageSize) {
		return fetcher.getPagedFeedItems(order, pageNum, pageSize);
	}

	public Page getPagedFansSubFeedItems(Fans fans, String order, int pageNum,
			int pageSize) {
		return fetcher.getPagedFansSubFeedItems(fans, order, pageNum, pageSize);
	}

	public Page getPagedFansFeedItems(Fans fans, String order, int pageNum,
			int pageSize) {
		return fetcher.getPagedFansFeedItems(fans, order, pageNum, pageSize);
	}

	public Page getPagedTagFeedItems(Tag tag, String order, int pageNum,
			int pageSize) {
		return fetcher.getPagedTagFeedItems(tag, order, pageNum, pageSize);
	}

	public Page getPagedFeedFeedItems(Feed feed, String order, int pageNum,
			int pageSize) {
		return fetcher.getPagedFeedFeedItems(feed, order, pageNum, pageSize);
	}

	public List getPagedGroups(int pageNum, int pageSize, String order) {
		return fetcher.getPagedGroups(pageNum, pageSize, order);
	}

	public List getPagedPostMessages(int pageNum, int pageSize, int fansId,
			String order) {
		return fetcher.getPagedPostMessages(pageNum, pageSize, fansId, order);
	}

	public List getPagedReceiveMessages(int pageNum, int pageSize, int fansId,
			String order) {
		return fetcher
				.getPagedReceiveMessages(pageNum, pageSize, fansId, order);
	}

	public Page getPagedRootTags(String order, int pageNum, int pageSize) {
		return fetcher.getPagedRootTags(order, pageNum, pageSize);
	}

	public Page getPagedFeed(int pageNum, int pageSize) {
		return fetcher.getPagedFeed(pageNum, pageSize);
	}

	public List getPagedTags(int pageNum, int pageSize, String order) {
		return fetcher.getPagedTags(pageNum, pageSize, order);
	}

	public List getRecentBoardItem(int i) {
		return fetcher.getRecentBoardItem(i);
	}

	public List getRecommendRootTags() {
		return fetcher.getRecommendRootTags();
	}

	public Object getRelArticles(Article article) {
		return fetcher.getRelArticles(article);
	}

	public List getRelFeedItems(FeedItem feedItem) {
		return fetcher.getRelFeedItems(feedItem);
	}

	public Tag getTag(int id) {
		return fetcher.getTag(id);
	}

	public Tag getTagByTitle(String relationTagTitle) {
		return fetcher.getTagByTitle(relationTagTitle);
	}

	public Tag addNewTagByTitle(Fans fans, String tagTitle) {
		fans.setPersistence(persistence);
		return fans.addNewTagByTitle(tagTitle);
	}

	/**
	 * 给用户增加对这个tag的使用记录。<br>
	 * 
	 * @param fans
	 * @param tag
	 */
	public void addFansTag(Fans fans, Tag tag) {
		fans.setPersistence(persistence);
		fans.addFansTag(tag);
	}

	public List getTagRelRootTag(Tag tag) {
		return fetcher.getTagRelRootTag(tag);
	}

	public List getUserArticle(String name) {
		return fetcher.getUserArticle(name);
	}

	public List listGroup() {
		return fetcher.listGroup();
	}

	public void remove(Object o) {
		persistence.remove(o);
	}

	/**
	 * 功能说明：用户离开小组，为方便使用事务，采用remove前缀<br>
	 * 创建者：张俊<br>
	 * 创建时间：2006-5-3<br>
	 * 
	 * @param group
	 * @param user
	 * @return
	 */
	public Group removeFansFromGroup(Group group, Fans user) {
		user.removeGroup(group);
		return group;
	}

	public void removeGroup(Group group) {
		group.setPersistence(persistence);
		group.remove();
	}

	//
	public void save(Object o) {
		persistence.save(o);
	}

	/**
	 * 用户新增或修改文章
	 * 
	 * @param fans
	 * @param article
	 * @param tag
	 */
	public void saveArticle(Fans fans, Article article, Tag tag) {
		// 给fans注入persistence
		fans.setPersistence(persistence);
		if (article.getArticleId() == 0) {// 新的文章
			fans.addArticle(article, tag);
		} else {
			fans.editArticle(article, tag);
		}
	}

	public void saveFans(Fans user) {
		user.setPersistence(persistence);
		user.save();
	}

	public void saveFeedBack(FeedBack feedBack) {
		feedBack.setPersistence(persistence);
		feedBack.save();
	}

	public void saveGroup(Group group) {
		group.setPersistence(persistence);
		group.save();
	}

	/**
	 * 功能说明：保存用户对软件的使用评价<br>
	 * 创建者：张俊<br>
	 * 创建时间：2006-5-23<br>
	 * 
	 * @param tagId
	 * @param status
	 */
	public void saveJudgeRootTag(String tagId, int status) {
		Tag tag = getTag(Integer.parseInt(tagId));
		Fans fans = getFans(ThreadFans.get().getFansId());
		fans.setPersistence(persistence);
		fans.judgeRootTag(status, tag);
	}

	/**
	 * 用户新增或修改软件
	 * 
	 * @param fans
	 * @param tag
	 */
	public void saveRootTag(Fans fans, RootTag tag) {
		fans.setPersistence(persistence);
		if (tag.getTagId() == 0) {// 新的软件
			tag.fill();
			fans.addRootTag(tag);
		} else {
			fans.editRootTag(tag);
		}
	}

	public List searchTags(String title) {
		return fetcher.searchTags(title);
	}

	public void setFetcher(Fetcher fetcher) {
		this.fetcher = fetcher;
	}

	public void setPersistence(Persistence persistence) {
		this.persistence = persistence;
	}

	public void addFansSubFeed(Fans fans, Feed feed) {
		fans.setPersistence(persistence);
		fans.subFeed(feed);
	}

	/**
	 * 用户新增feed
	 * 
	 * @param fans
	 * @param feed
	 */
	public void saveFeed(Fans fans, Feed feed) {
		fans.addFeed(feed);
		feed.setFans(fans);
		save(feed);
		fans.setPersistence(persistence);
		fans.subFeed(feed);
	}

	public List getTagFeedItem(Tag tag) {
		return fetcher.getTagFeedItem(tag);
	}

	/**
	 * Delete feed the fans has subscribed Author:pesome<br>
	 * Date:2007-1-24<br>
	 * 
	 * @param fans
	 * @param feed
	 */
	public void removeFansSubFeed(Fans fans, Feed feed) {
		fans.setPersistence(persistence);
		fans.unsubFeed(feed);
	}

	public void addFansSavedArticle(Fans fans, Article article) {
		fans.publishArticle(article);
	}

	public void removeFansSavedArticle(Fans fans, Article article) {
		fans.unPublishArticle(article);
	}

	public void saveArticle(Fans fans, String tagStr, Article article) {
		Set<Tag> tags = getTagSet(tagStr, fans);
		fans.setPersistence(persistence);
		fans.saveArticle(tags, article);

	}

	public List getRecentRootTags(int pageSize, String order) {
		return fetcher.getRecentRootTags(pageSize, order);
	}

	public List getRecentFans(int pageSize, String order) {
		return fetcher.getRecentFans(pageSize, order);
	}

	public void addFansSnippet(FansSnippet fs) {
		fs.setPersistence(persistence);
		String url = fs.getSnippet().getUrl();
		Snippet s = fetcher.getSnippetByUrl(url);

		// 该url已经被摘录过
		if (null != s) {
			fs.saveExitSnippet(s);
		} else {
			fs.saveNewSnippet();
		}
	}

	public Page getPagedFansSnippet(Fans fans, String query, String order,
			int pageNum, int pageSize) {
		return fetcher.getPagedFansSnippet(fans, query, order, pageNum,
				pageSize);
	}

	public Page getPagedSnippet(String query, String order, int pageNum,
			int pageSize) {
		return fetcher.getPagedSnippet(query, order, pageNum, pageSize);
	}

	public Snippet getSnippet(int id) {
		return fetcher.getSnippet(id);
	}

	public Page getPagedRootTagRemarks(RootTag tag, String order, int pageNum,
			int pageSize) {
		// TODO Auto-generated method stub
		return fetcher.getPagedRootTagRemarks(tag, order, pageNum, pageSize);
	}

	public void digg(RootTag tag, Fans fans) {
		fans.setPersistence(persistence);
		fans.diggRootTag(tag);
	}
	
	public List getFeedsByTag(Tag tag){
		return fetcher.getFeedsByTag(tag);
	}
	
	public List getSnippetsByTag(Tag tag){
		return fetcher.getSnippetsByTag(tag);
	}
}
